package com.example.qxfw.shfx.Controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.yj.SlWarning;
import com.example.qxfw.common.util.DateUtil;
import com.google.common.primitives.Bytes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.example.qxfw.shfx.Service.YjcxService;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "山洪防汛--预警查询")
@CrossOrigin
@RestController
@RequestMapping("/AlertQueryController")
public class AlertQueryController {

    @Autowired
    private YjcxService yjcxService;

    @Value("${filePath.shdqFilePath}")
    private String shdqFilePath;

    @Value("${config.winpath}")
    private String winpath1;
    @Value("${config.linuxpath}")
    private String linuxpath1;
    @Value("${config.dbname}")
    public String dbname1;


    @ApiOperation("山洪防汛--预警查询-预警查询")
    @GetMapping("/selectByTimePeriodOfSlWarning")
    public SysResult selectByTimePeriodOfSlWarning(String start, String end, Integer state){
        Timestamp startDate = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(start));
        Timestamp endDate = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(end));
        List<SlWarning> list = yjcxService.selectByTimePeriodOfSlWarning(startDate, endDate, state);
        return SysResult.success(list);
    }

    @ApiOperation("山洪防汛--预警查询-审核产品")
    @GetMapping("/updateState")
    public SysResult updateState(int DOCId, int state){
        SlWarning slWarning = yjcxService.updateState(DOCId, state);
        return SysResult.success(slWarning);
    }

    @ApiOperation("山洪防汛--预警查询-预警删除")
    @DeleteMapping("/deleteByIdOfSlWarning")
    public SysResult deleteByIdOfSlWarning(int DOCId){
        List<SlWarning> list = yjcxService.deleteByIdOfSlWarning(DOCId);
        return SysResult.success(list);
    }

    @ApiOperation("山洪防汛--预警查询-获取id")
    @GetMapping("/selectProductId")
    public SysResult selectProductId(int docId,String docPath, HttpServletResponse response) throws IOException {
        Integer productId = yjcxService.getProductId(docId, docPath);
        return SysResult.success(productId);
    }
    @ApiOperation("网络请求资源")
    @PostMapping("/getImg")
    public byte[] getResource(String localResource) {

         String filePath = shdqFilePath+localResource;

        String resource = filePath;
        List<Byte> resource2 = new ArrayList<>();
        if(new File(resource).exists()) {
            try(BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(Paths.get(resource)))) {
                byte[] buff = new byte[1024];
                int len;
                while ((len = bis.read(buff)) != -1) {
                    for (int i = 0; i < len; i++) {
                        resource2.add(buff[i]);
                    }
                }
                return Bytes.toArray(resource2);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

}
